package mxx.mr.job;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @program:report-form-hadoop
 * @description:月度不合格品统计表 Job
 * @author:AlTuring
 * @Date:2019/11/8-10:08
 **/
public class QuailityMonthUnqualifiedJob {
    /**
     * 齿轮车间月度不合格品统计表
     */
    public static class QuailityMonthUnqualifiedMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] datas = value.toString().split(",");
            //工件编码
            String workpieceCode = datas[0];
            //工序编码
            String processSEQ = datas[3];
            //检验时间
            String time = datas[5].substring(0,7);

            // id Hash key(工件编码-物料名称-车间-工序名称-时间)
            Text outKey = new Text(workpieceCode + "_" +
                    processSEQ + "_" +
                    time);

            context.write(outKey, value);
        }
    }
    /**
     * 齿轮车间月度不合格品统计表
     */
    public static class QuailityMonthUnqualifiedReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //工件编码
            String productCode = "";
            //物料名称
            String materialName = "";
            //批次号
            String batch = "#";
            //车间
            String workshop = "chilun";
            //工序名称
            String processName = "";
            //不合格总数
            int total = 0;
            //生产指令单编码
            String planCode = "#";
            //质量状态
            String qualityStatus = "不合格";
            //时间(2019-11)
            String time = "";
            for (Text value : values) {
                String[] datas = value.toString().split(",");
                productCode = datas[0];
                materialName = datas[1];
                processName = datas[4];
                time = datas[5].substring(0,7);
                total++;
            }
            //自定义outValue
            Text outValue = new Text(productCode +","+
                    materialName +","+
                    batch +","+
                    workshop +","+
                    processName +","+
                    total +","+
                    planCode +","+
                    qualityStatus +","+
                    "不良品特征,"+
                    time);

            // -- 使用联合key的hash散列作为key
            Text outKey = new Text(DigestUtils.shaHex(key.toString()));

            context.write(outKey, outValue);
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("mapred.textoutputformat.separator", ",");

        // 如果输出目录已经存在，则先删除
        FileSystem fileSystem = FileSystem.get(configuration);

        // 输出文件
        Path outputPath = new Path(args[1]);
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        // 输入文件
        Path inputPath = new Path(args[0]);

        Job job = Job.getInstance(configuration);
        job.setJarByClass(QuailityMonthUnqualifiedJob.class);

        job.setMapperClass(QuailityMonthUnqualifiedMapper.class);
        job.setReducerClass(QuailityMonthUnqualifiedReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);

        job.waitForCompletion(true);

        System.out.println("---齿轮车间月度不合格品统计表完成---");
    }
}
